import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
const login = () => import('../pages/login/login.vue')
const layout = () => import('../pages/index/layout.vue')
const chart = () => import('../pages/index/chart.vue')
const menu = () => import('../pages/menu/index.vue')
const menuInfo = () => import('../pages/menu/info.vue')
const role = () => import('../pages/role/index.vue')
const roleInfo = () => import('../pages/role/info.vue')
const admin = () => import('../pages/admin/index.vue')
const adminInfo = () => import('../pages/admin/info.vue')
const cate = () => import('../pages/cate/index.vue')
const goods = () => import('../pages/goods/index.vue')
const phone = () => import('../phone/index.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: {
        path: '/index'
      }
    },
    {
      path: '/phone',
      component: phone
    },
    {
      path: '/index',
      name: 'layout',
      component: layout,
      children: [
        // 图表页面
        {path: '',component: chart},
        // 菜单列表
        {path: 'menu',component: menu},
        // 菜单添加
        {path: 'menu/add',component: menuInfo},
        // 菜单编辑,动态路由
        {path: 'menu/:id',component: menuInfo},
        // 角色列表
        {path: 'role',component: role},
        // 角色添加
        {path: 'role/add',component: roleInfo},
        // 角色编辑,动态路由
        {path: 'role/:id',component: roleInfo},
        // 角色列表
        {path: 'admin',component: admin},
        // 角色添加
        {path: 'admin/add',component: adminInfo},
        // 角色编辑,动态路由------uid-----------
        {path: 'admin/:uid',component: adminInfo},
        // 商品分类列表
        {path: 'cate',component: cate},
        // 商品管理列表
        {path: 'goods',component: goods},
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: login
    }, 
    // 404处理
    // {
      
    // }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '后台管理系统';
  // 判断路由
  if(to.path === '/login') {
    // 登录页不做拦截
    next()
  }else {
    // 不去登录页，需要判断是否登录
    if(localStorage.getItem('userInfo')) {
      // localStorage有数据，说明已经登录
      next()
    }else {
      // localStorage没有数据，说明没有登录，就去登录
      next('/login')
    }
  }
})

export default router
